#ifndef __CC_MQTT__
#define __CC_MQTT__

#include <PubSubClient.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>

#include "cc_core.h"
#include "cc_log.h"
#include "cc_msg.h"

typedef void (*mqtt_topic_handle_cb)(char* topic, char* msg, int length, int topic_len);

typedef struct
{
  char topic[CC_CONFIG_MSG_TOPIC_MAX_LEN];
  mqtt_topic_handle_cb cb;
} mqtt_topic_handle;

void cc_topic_subsribe(mqtt_topic_handle_cb cb, char* topic);

#define CC_TOPIC_SUBSRIBE_ALIAS(cb, topic)                  \
  void __attribute__((constructor)) cc_topic_init##cb(void) \
  {                                                         \
    cc_topic_subsribe(cb, topic);                           \
  }

#define CC_TOPIC_SUBSRIBE(cb) \
  CC_TOPIC_SUBSRIBE_ALIAS(cb, #cb)

// 注册一个组件初始化函数
void cc_component_register(cc_component_fun func, int prio, const char* name);

#define CC_MQTT_CONNECTION_TIMEOUT      (MQTT_CONNECTION_TIMEOUT)
#define CC_MQTT_CONNECTION_LOST         (MQTT_CONNECTION_LOST)
#define CC_MQTT_CONNECT_FAILED          (MQTT_CONNECT_FAILED)
#define CC_MQTT_DISCONNECTED            (MQTT_DISCONNECTED)
#define CC_MQTT_CONNECTED               (MQTT_CONNECTED)
#define CC_MQTT_CONNECT_BAD_PROTOCOL    (MQTT_CONNECT_BAD_PROTOCOL)
#define CC_MQTT_CONNECT_BAD_CLIENT_ID   (MQTT_CONNECT_BAD_CLIENT_ID)
#define CC_MQTT_CONNECT_UNAVAILABLE     (MQTT_CONNECT_UNAVAILABLE)
#define CC_MQTT_CONNECT_BAD_CREDENTIALS (MQTT_CONNECT_BAD_CREDENTIALS)
#define CC_MQTT_CONNECT_UNAUTHORIZED    (MQTT_CONNECT_UNAUTHORIZED)

int cc_msg_mqtt_init();
int cc_msg_mqtt_publish(const char* topic, const char* data);
void cc_msg_mqtt_subsribe(const char* topic);
int cc_get_mqtt_state();

#endif